#include "deallogicobj.h"
#include "dbcontroler.h"
#include "mytcpserver.h"
#include "tcpuserconnect.h"
#include "WorkServe/userservice.h"
#include "WorkServe/searchservice.h"
#include "WorkServe/friendservice.h"
DealLogicObj::DealLogicObj(QObject *parent) : QObject(parent)
{
    UserServer=new UserService(this);
    SearchServer=new SearchService(this);
    FriendServer=new FriendService(this);
    Work();
}

void DealLogicObj::DoHandler(myuint Sig, std::shared_ptr<PDU>Msg, TcpUserConnect *UserPtr)
{
    if(! WorkHandler.count(Sig)){
        return;
    }
    WorkHandler[Sig](Msg,UserPtr);
}

DealLogicObj &DealLogicObj::getInstance()
{
    static DealLogicObj instance;
    return instance;
}

void DealLogicObj::RegisterWork(myuint Sig, std::function<void (std::shared_ptr<PDU>, TcpUserConnect *)> Handler)
{
    WorkHandler[Sig]=Handler;
}

void DealLogicObj::Work()
{
    //用户操作服务
    RegisterWork(MSG_TYPE_USER_REGISER_REQUEST,[&](std::shared_ptr<PDU> Msg, TcpUserConnect * tcpUser){
        UserServer->_doUserRegister(Msg,tcpUser);
    });
    RegisterWork(MSG_TYPE_USER_LOGIN_REQUEST,[&](std::shared_ptr<PDU> Msg, TcpUserConnect * tcpUser){
        UserServer->_doUserLogin(Msg,tcpUser);
    });
    //查询操作服务
    RegisterWork(MSG_TYPE_SEARCH_ONLINE_REQUEST,[&](std::shared_ptr<PDU> Msg, TcpUserConnect * tcpUser){
        SearchServer->_doFindOnline(Msg,tcpUser);
    });
    RegisterWork(MSG_TYPE_ALLNET_SCAN_ALL_REQUEST,[&](std::shared_ptr<PDU> Msg, TcpUserConnect * tcpUser){
        SearchServer->_doAllNetScanAll(Msg,tcpUser);
    });
    //好友操作服务
    RegisterWork(MSG_TYPE_ADD_FRIEND_REQUEST,[&](std::shared_ptr<PDU> Msg, TcpUserConnect * tcpUser){
        FriendServer->_SenderDoAddFriend(Msg,tcpUser);
    });
    RegisterWork(MSG_TYPE_FRIEND_INFORM_PULL_REQUEST,[&](std::shared_ptr<PDU> Msg, TcpUserConnect * tcpUser){
        FriendServer->_doPullAddFriendRequest(Msg,tcpUser);
    });
    RegisterWork(MSG_TYPE_ADD_FRIEND_RESPOND,[&](std::shared_ptr<PDU> Msg, TcpUserConnect * tcpUser){
        FriendServer->_doSetFriendRespond(Msg,tcpUser);
    });
    RegisterWork(MSG_TYPE_PULL_FRIEND_LIST_REQUEST,[&](std::shared_ptr<PDU> Msg,TcpUserConnect * tcpUser){
        FriendServer->_doPullFriendListRespond(Msg,tcpUser);
    });

}

